/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: Feb 29, 2012
 */
package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Job;
import model.StaffMember;
import model.Volunteer;

import controller.Controller;

/**
 * This Panel is a Panel which shows all of a job's information.
 * @author Michael Satran Jr
 */
public class JobInformationPanel extends JPanel{
	
	/**
	 * This is the job to be shown in the JobInformationPanel.
	 */
	private Job my_job;
	/**
	 * Reference to the controller.
	 */
	private Controller my_controller;
	
	/**
	 * Contructs a new JobInformationPanel
	 * @param the_job The job to display on this panel.
	 * @param the_controller A reference to the controller.
	 */
	public JobInformationPanel(Job the_job, Controller the_controller) {
		super();
		my_controller = the_controller;
		my_job = the_job;
	}
	
	/**
	 * Sets this panel up by adding buttons etc.
	 */
	public void setup() {
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel[] lines = new JPanel[6];
		
		for (int i = 0; i < lines.length - 1; i++) {
			lines[i] = new JPanel();
			lines[i].setLayout(new BorderLayout());
			JLabel new_label = new JLabel();
			JLabel data_label = new JLabel();
			switch(i) {
			case 0:
				new_label.setText("Park Name: ");
				data_label.setText(my_job.getParkName());
				break;
			case 1:
				new_label.setText("Start Date: ");
				data_label.setText(my_job.getStartDate().toString());
				break;
			case 2:
				new_label.setText("End Date: ");
				data_label.setText(my_job.getEndDate().toString());
				break;
			case 3:
				new_label.setText("Job Description: ");
				data_label.setText(my_job.getJobDescription());
				break;
			case 4:
				new_label.setText("Job Difficulty: ");
				data_label.setText(my_job.getDifficultyInfo());
				break;
			}
			lines[i].add(new_label, BorderLayout.WEST);
			lines[i].add(data_label, BorderLayout.CENTER);
			this.add(lines[i]);
		}
		
		lines[5] = new JPanel(new BorderLayout());
		lines[5].add(new JLabel("Volunteers:"), BorderLayout.NORTH);
		final JList volunteers_list = new JList(my_job.getVolunteers().toArray());
		lines[5].add(volunteers_list, BorderLayout.CENTER);
		
		JPanel buttons_container = new JPanel();
		
		JButton blackball_button = new JButton("Blackball Selected Volunteer");
		JButton unblackball_button = new JButton("Unblackball Selected Volunteer");
		JButton volunteer_information_button = new JButton("Show Selected Volunteer Information");
		
		blackball_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Volunteer volunteer = (Volunteer)volunteers_list.getSelectedValue();
				if (volunteer != null) {
					my_controller.blackBallVolunteer(volunteer);
				} else {
					JOptionPane.showMessageDialog(null, "A volunteer has not been selected!");
				}
			}
			
		});
		
		unblackball_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Volunteer volunteer = (Volunteer)volunteers_list.getSelectedValue();
				if (volunteer != null) {
					my_controller.unBlackBallVolunteer(volunteer);
				} else {
					JOptionPane.showMessageDialog(null, "A volunteer has not been selected!");
				}
			}
			
		});
		
		volunteer_information_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Volunteer volunteer = (Volunteer)volunteers_list.getSelectedValue();
				if (volunteer != null) {
					VolunteerInfoPanel volunteer_panel = new VolunteerInfoPanel(volunteer);
					JFrame frame = new JFrame("Volunteer Information for: " + volunteer.getFirstName() + " " + volunteer.getLastName());
					volunteer_panel.setup();
					frame.setContentPane(volunteer_panel);
					frame.pack();
					frame.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(null, "A volunteer has not been selected!");
				}
			}
			
		});

		if (my_controller.getCurrentUser() instanceof StaffMember) {
			buttons_container.add(blackball_button);
			buttons_container.add(unblackball_button);
		}
		
		
		buttons_container.add(volunteer_information_button);
		
		lines[5].add(buttons_container, BorderLayout.SOUTH);
		this.add(lines[5]);
	}
	
	

}
